# Check average number of posts by day for winner and loser parties.
# See Appendix G.




#############
# load data #
#############

library(brms)
library(ggplot2)
library(ggpubr)
library(stargazer)

load("PSRM Replication Files/PresidentialElectionsPosts.RData")

colnames(president_df)

length(unique(president_df$country)) # 26
length(unique(president_df$electionname)) # 29
length(unique(president_df$party_country)) # 52

# focus on 15 days before and after the election
day_range <- 15

sub <- president_df[which(president_df$daysinceelection >= -1*day_range
                          & president_df$daysinceelection <= day_range),]

# winners and losers
winners <- sub[which(sub$win == 1),]
losers <- sub[which(sub$win != 1),]

length(unique(winners$electionname[which(!is.na(winners$polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$economic_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$social_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_present))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_dummy))])) # 27

length(unique(losers$electionname[which(!is.na(losers$polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$economic_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$social_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_present))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_dummy))])) # 27




##########################
# average # posts by day #
##########################

aveposts <- data.frame(Type=rep(c("Winners", "Losers"), each=31),
                       dayssinceelection=-15:15)
aveposts$Type <- factor(aveposts$Type, levels=unique(aveposts$Type))

# compute average number of posts
aveposts$nposts[1:31] <- as.vector(table(winners$daysinceelection) / tapply(winners$party_election, winners$daysinceelection, function(p){
  length(unique(p))
}))

aveposts$nposts[32:62] <- as.vector(table(losers$daysinceelection) / tapply(losers$party_election, losers$daysinceelection, function(p){
  length(unique(p))
}))

# compute average love and angry proportions
aveposts$aveloveprop[1:31] <- tapply(winners$loveprop, winners$daysinceelection, mean)
aveposts$aveloveprop[32:62] <- tapply(losers$loveprop, losers$daysinceelection, mean)
aveposts$aveangryprop[1:31] <- tapply(winners$angryprop, winners$daysinceelection, mean)
aveposts$aveangryprop[32:62] <- tapply(losers$angryprop, losers$daysinceelection, mean)

# figure G.1
ggplot(data = aveposts, 
       aes(x = dayssinceelection, y = nposts, colour = Type)) +
  geom_vline(xintercept=0, lwd=0.5, lty=2) +
  geom_line(lwd = 1.7) +
  scale_color_manual(values=c("black", "gray")) +
  xlab("Days since the Election (±15 Days)") +
  ylab("Average Number of Posts") +
  theme_bw() +
  theme(axis.title.x = element_text(size = 15),
        axis.title.y = element_text(size = 15),
        axis.text=element_text(size=12))

#ggsave("averageposts.pdf", width=12, height=6)
